//
//  ViewController.m
//  30103Chara
//
//  Created by student on 16/3/17.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    BOOL bMove;
    CGPoint pointXY;//距离中心的偏移量
}
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic,strong) NSArray *arr;
@property (nonatomic,assign) CGPoint  point;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //创建图片数组
//    NSMutableArray *mArr = [[NSMutableArray alloc]init];
//    for(int i=1;i<=2;i++)
//    {
//        NSString *picName = [NSString stringWithFormat:@"chara%i.png",i];
//        UIImage *image = [UIImage imageNamed:picName];
//        [mArr addObject:image];
//       
//        
//    }

//    self.imageView.animationImages = mArr;
    self.imageView.animationDuration = 0.3;
    [self.imageView startAnimating];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.imageView.highlighted = !self.imageView.highlighted;
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    self.point = point;
    //得到触摸点是否在imageView范围内
//    bMove = CGRectContainsPoint(self.imageView.frame, point);
//    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moveV:) userInfo:nil repeats:YES];
//        [self.imageView startAnimating];
    CGPoint center = self.imageView.center;
    
    if(center.x > self.point.x)
    {
        center.x -= 30;
    }
    
    if(center.x < self.point.x)
    {
        center.x += 30;
    }
    
    if(center.y >self.point.y)
    {
        center.y -=20;
    }
    if(center.y < self.point.y)
    {
        center.y += 20;
    }
    self.imageView.center = center;
    if(center.x == self.point.x)
    {
        [self.imageView stopAnimating];
    }

    
}

                                                                          
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
//    if(!bMove)
//    {
//
//        UITouch *touch = [touches anyObject];
//        CGPoint point = [touch locationInView:self.view];
//        self.imageView.center = CGPointMake(point.x - pointXY.x, point.y - pointXY.y);
//        
//    }
}
//-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    
//    [self.imageView stopAnimating];
//}
//    //轻扫手势
//    UISwipeGestureRecognizer *swipeR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDo:)];
//    swipeR.direction = UISwipeGestureRecognizerDirectionRight;
//    swipeR.numberOfTouchesRequired = 1;
//    [self.imageView addGestureRecognizer:swipeR];
//    
//    UISwipeGestureRecognizer *swipeL = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDo:)];
//    swipeL.direction = UISwipeGestureRecognizerDirectionLeft;
//    swipeL.numberOfTouchesRequired = 1;
//    [self.imageView addGestureRecognizer:swipeL];
//    
//    UISwipeGestureRecognizer *swipeD = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDo:)];
//    swipeD.direction = UISwipeGestureRecognizerDirectionDown;
//    swipeD.numberOfTouchesRequired = 1;
//    [self.imageView addGestureRecognizer:swipeD];
//    
//    UISwipeGestureRecognizer *swipeU = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDo:)];
//    swipeU.direction = UISwipeGestureRecognizerDirectionUp;
//    swipeU.numberOfTouchesRequired = 1;
//    [self.imageView addGestureRecognizer:swipeU];
//}
//-(void)swipeDo:(UISwipeGestureRecognizer *)g
//{
//
//    switch (g.direction) {
//        case UISwipeGestureRecognizerDirectionRight:
//            self.imageView.frame = CGRectOffset(self.imageView.frame, 50, 0);
//            break;
//        case UISwipeGestureRecognizerDirectionLeft:
//            self.imageView.frame = CGRectOffset(self.imageView.frame, -50, 0);
//            break;
//        case UISwipeGestureRecognizerDirectionDown:
//            self.imageView.frame = CGRectOffset(self.imageView.frame, 0, 50);
//            break;
//        case UISwipeGestureRecognizerDirectionUp:
//            self.imageView.frame = CGRectOffset(self.imageView.frame, 0,-50);
//
//            break;
//           }
//    
//}

@end
